package wangjg.commons.io;

/**
 * 低位在前实现
 * @author wangjg
 *
 */
public class Smallend {

	public static byte[] InttoByteArray(int n) {
		byte[] b = new byte[4];
		b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		b[2] = (byte) (n >> 16 & 0xff);
		b[3] = (byte) (n >> 24 & 0xff);
		return b;
	}

	public static byte[] ShorttoByteArray(short n) {
		byte[] b = new byte[2];
		b[1] = (byte) (n & 0xff);
		b[0] = (byte) (n >> 8 & 0xff);
		return b;
	}

	public static int ByteArraytoInt(byte[] b) {
		int iOutcome = 0;
		byte bLoop;
		for (int i = 0; i < 4; i++) {
			bLoop = b[i];
			iOutcome += (bLoop & 0xff) << (8 * i);
		}
		return iOutcome;
	}

	public static short ByteArraytoShort(byte[] b) {
		short iOutcome = 0;
		byte bLoop;
		for (int i = 0; i < 2; i++) {
			bLoop = b[i];
			iOutcome += (bLoop & 0xff) << (8 * i);
		}
		return iOutcome;
	}
	
	public static void main(String args[]){
		int n=2;
		byte[] buf=InttoByteArray(n);
		buf[0]=0x4D;
		int n2 = ByteArraytoInt(buf);
		for(byte b:buf){
			System.out.println("b="+b);
		}
	}
}
